/**
 * 
 */
package cn.micai.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.micai.common.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.image.ImageCaptchaService;

import cn.micai.common.web.ResponseUtils;
import cn.micai.core.bean.country.City;
import cn.micai.core.bean.country.Province;
import cn.micai.core.bean.country.Town;
import cn.micai.core.bean.user.Buyer;
import cn.micai.core.query.country.CityQuery;
import cn.micai.core.query.country.TownQuery;
import cn.micai.core.service.country.CityService;
import cn.micai.core.service.country.ProvinceService;
import cn.micai.core.service.country.TownService;
import cn.micai.core.service.user.BuyerService;
import cn.micai.core.web.Constants;

/**
 * 跳转登录页面
 * 登录
 * 注册
 * 个人资料
 * 退出
 * 收获地址
 * 
 * @author 赵新国
 *
 */
@Controller
public class ProfileController extends BaseController{
	
	@Autowired
	private BuyerService buyerService;
	
	@Autowired
	private ImageCaptchaService imageCaptchaService;
	
	@Autowired
	private ProvinceService provinceService;
	
	@Autowired
	private CityService cityService;
	
	@Autowired
	private TownService townService;

	//GET
	@RequestMapping(value = "/shopping/login.shtml", method = RequestMethod.GET)
	public String login(){
		return "buyer/login";
	}
	
	//GET
	@RequestMapping(value = "/shopping/register.shtml", method = RequestMethod.GET)
	public String register(){
		return "buyer/register";
	}
	
	//POST
	/**
	 * 1:验证码是否为null 
	 * 2:验证码 是否正确 
	 * 3:用户是否为NUll 
	 * 4:密码是否为NUll 
	 * 5:用户是否正确 
	 * 6密码是否正确 Md5
	 * 纯生Md5 放进Session 跳转ReturnUrl
	 * 
	 * @param buyer
	 * @param captcha
	 * @param returnUrl
	 * @return
	 */
	@RequestMapping(value = "/shopping/login.shtml", method = RequestMethod.POST)
	public String login(Buyer buyer,String captcha,String returnUrl,ModelMap model,HttpServletRequest request){
		//1:验证码是否为null 
		if(StringUtils.isNotBlank(captcha)){
			//1.JSESSIONID
			//2.验证码
			if(imageCaptchaService.validateResponseForID(sessionProvider.getSessionId(request), captcha)){
				//3.用户是否为NUll
				if(null != buyer && StringUtils.isNotBlank(buyer.getUsername())){
					//4:密码是否为NUll
					if(StringUtils.isNotBlank(buyer.getPassword())){
						Buyer b = buyerService.getBuyerByKey(buyer.getUsername());
						if(null != b){
							//6:密码是否正确 Md5
							if(b.getPassword().equals(mD5Pwd.encode(buyer.getPassword()))){
								//把用户对象 放进Session 
								sessionProvider.setAttribute(request, Constants.BUYER_SESSION, b);
								//跳转ReturnUrl
								if (StringUtils.isNotBlank(returnUrl)) {
									return "redirect:" + returnUrl;
								} else {
									// 个人中心
									return "redirect:/buyer/index.shtml";
								}
							}else{
								model.addAttribute("error", "密码输入错误");
							}
						}else{
							model.addAttribute("error", "用户名输入错误");
						}
					}else{
						model.addAttribute("error", "请输入密码");
					}
				}else{
					model.addAttribute("error", "请输入用户名");
				}
			}else{
				model.addAttribute("error", "验证码输入错误");
			}
		}else{
			model.addAttribute("error", "请填写验证码");
		}
		return "buyer/login";
	}
	
	//POST
	/**
	 * 1:验证码是否为null 
	 * 2:验证码 是否正确 
	 * 3:用户是否为NUll 
	 * 4:密码是否为NUll 
	 * @param buyer
	 * @param captcha
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/shopping/register.shtml", method = RequestMethod.POST)
	public String register(Buyer buyer,String captcha,ModelMap model,HttpServletRequest request){
		//1:验证码是否为null 
		if(StringUtils.isNotBlank(captcha)){
			//1.JSESSIONID
			//2.验证码
			if(imageCaptchaService.validateResponseForID(sessionProvider.getSessionId(request), captcha)){
				//3.用户是否为NUll
				if(null != buyer && StringUtils.isNotBlank(buyer.getUsername())){
					//4:密码是否为NUll
					if(StringUtils.isNotBlank(buyer.getPassword())){
						//5.重复密码是否为NULL
						if(StringUtils.isNotBlank(buyer.getRpassword())){
							//6.密码和重复密码是否相等
							if(!buyer.getPassword().equals(buyer.getRpassword())){
								model.addAttribute("error", "两次密码不一致");
							}else{
								//7.密码 Md5
								buyer.setPassword(mD5Pwd.encode(buyer.getPassword()));
								Integer result = buyerService.addBuyer(buyer);
								if(result > 0){
									model.addAttribute("message", "注册成功");
									// 登录页面
									return "redirect:/buyer/login.shtml";
								}
							}
						}else{
							model.addAttribute("error", "请输入重复密码");
						}
					}else{
						model.addAttribute("error", "请输入密码");
					}
				}else{
					model.addAttribute("error", "请输入用户名");
				}
			}else{
				model.addAttribute("error", "验证码输入错误");
			}
		}else{
			model.addAttribute("error", "请填写验证码");
		}
		return "buyer/register";
	}
	
	//我的订单(个人中心)
	@RequestMapping(value = "/buyer/index.shtml")
	public String index(){
		return "buyer/index";
	}
	
	//我的订单，退换货订单，我的收藏(个人中心)
	@RequestMapping(value = "/buyer/orders.shtml")
	public String orders(){
		return "buyer/orders";
	}
	
	//个人资料
	@RequestMapping(value = "/buyer/profile.shtml")
	public String profile(HttpServletRequest request,ModelMap model){
		//加载用户
		Buyer buyer = (Buyer) sessionProvider.getAttribute(request, Constants.BUYER_SESSION);
		Buyer b = buyerService.getBuyerByKey(buyer.getUsername());
		model.addAttribute("buyer", b);
		//省
		List<Province> provinces = provinceService.getProvinceList(null);
		model.addAttribute("provinces", provinces);
		//市
		CityQuery cityQuery = new CityQuery();
		cityQuery.setProvince(b.getProvince());
		List<City> citys = cityService.getCityList(cityQuery);
		model.addAttribute("citys", citys);
		//县
		TownQuery townQuery = new TownQuery();
		townQuery.setCity(b.getCity());
		List<Town> towns = townService.getTownList(townQuery);
		model.addAttribute("towns", towns);
		
		return "buyer/profile";
	}
	
	//加载市
	@RequestMapping(value = "/buyer/city.shtml")
	public void city(String code,HttpServletResponse response){
		CityQuery cityQuery = new CityQuery();
		cityQuery.setProvince(code);
		List<City> citys = cityService.getCityList(cityQuery);
		JSONObject jo = new JSONObject();
		jo.put("citys", citys);
		ResponseUtils.renderJSON(response, jo.toString());
	}
	
	//加载县/区
	@RequestMapping(value = "/buyer/town.shtml")
	public void town(String code,HttpServletResponse response){
		TownQuery townQuery = new TownQuery();
		townQuery.setCity(code);
		List<Town> towns = townService.getTownList(townQuery);
		JSONObject jo = new JSONObject();
		jo.put("towns", towns);
		ResponseUtils.renderJSON(response, jo.toString());
	}
	
	//收货地址
	@RequestMapping(value = "/buyer/deliver_address.shtml")
	public String deliver_address(){
		
		return "buyer/deliver_address";
	}
	
	//修改密码
	@RequestMapping(value = "/buyer/change_password.shtml")
	public String change_password(){
		
		return "buyer/change_password";
	}
	
	//退出
	@RequestMapping(value = "/shopping/logout.shtml")
	public String logout(HttpServletRequest request,String returnUrl){
		sessionProvider.logout(request);
		return "redirect:" + returnUrl;
	}
	
}
